package com.flong.selenium;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.flong.common.IoUtil;

/**
 * http://www.seleniumhq.org/docs/03_webdriver.jsp
 * https://stackoverflow.com/questions/37785686/how-to-use-the-gecko-executable-with-selenium
 * 
 * @author jilongliang
 */

@SuppressWarnings("all")
public class ZhiHuTest2 {
	static WebDriver driver;
	static Wait<WebDriver> wait;

	public static void main(String[] args) throws Exception {
		System.setProperty("webdriver.gecko.driver", "C:\\Python27\\geckodriver.exe");
		DesiredCapabilities cap = DesiredCapabilities.firefox();
		cap.setCapability("marionette", true);
		cap.setBrowserName("firefox");
		driver = new FirefoxDriver();																// enable
																							// JS
		wait = new WebDriverWait(driver, 3000);
		final String url = "https://www.zhihu.com/lives/735803355447721984/participants";

		try {
			driver.navigate().to(url);
			
			JavascriptExecutor js = (JavascriptExecutor) driver;
			js.executeScript("window.scrollTo(0,1113500)");//向下拉到底
			js.executeScript("window.scrollBy(0,1113500)");
			
			Thread.sleep(16800*10);
			WebElement findElement = driver.findElement(By.id("liveParticipants:735803355447721984"));
			List<WebElement> cards = findElement.findElements(By.className("Card-card-102t"));
			String userItemNames="";
			for (WebElement card :cards) {
				WebElement userItemName = card.findElement(By.className("UserItem-name-1Tj7"));
				System.out.println(userItemName.getText());
				userItemNames +=userItemName.getText();
			}
			
			IoUtil.Writer(userItemNames, "D:/data/zhihui005.txt");
		} finally {
			driver.close();
		}
	}
}